{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0215c8ad-7c42-4aa4-b62d-478a64d031b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "信计221 刘显婷 224180117\n"
     ]
    }
   ],
   "source": [
    "from pyecharts.charts import Map\n",
    "from pyecharts import options as opts\n",
    "import re\n",
    "# 读取数据文件\n",
    "with open('湖南省数据.txt', 'r', encoding='utf-8') as f:\n",
    "    content = f.read()\n",
    "# 提取2020年商品销售额数据\n",
    "sales_pattern = r'2020年商品销售额：\\s*\\[(.*?)\\]'\n",
    "sales_match = re.search(sales_pattern, content, re.DOTALL)\n",
    "if sales_match:\n",
    "    sales_data_str = sales_match.group(1)\n",
    "    sales_data = eval(f'[{sales_data_str}]')\n",
    "else:\n",
    "    raise ValueError('未找到2020年商品销售额数据')\n",
    "# 提取地级市名称和销售额\n",
    "city_sales = [(city, sales) for city, sales in sales_data]\n",
    "# 提取主要经纬度坐标数据（假设每个地级市对应一个坐标，这里数据不太清晰，需要进一步明确规则，当前只是示例处理）\n",
    "coordinate_pattern = r'\\[\\[(\\d+\\.\\d+),(\\d+\\.\\d+)\\]\\]'\n",
    "coordinate_matches = re.findall(coordinate_pattern, content)\n",
    "coordinates = [(float(lng), float(lat)) for lng, lat in coordinate_matches]\n",
    "# 构建城市和坐标的映射关系（假设顺序对应，实际需要根据正确逻辑处理）\n",
    "city_coordinate_map = dict(zip([city for city, _ in city_sales], coordinates))\n",
    "# 创建Map对象\n",
    "map_chart = Map()\n",
    "# 添加数据和设置地图属性\n",
    "map_chart.add(\"2020年湖南省各地级市商品销售额\", [list(item) for item in city_sales], \"湖南\")\n",
    "# 设置全局配置项\n",
    "map_chart.set_global_opts(\n",
    "    title_opts=opts.TitleOpts(title=\"2020年湖南省各地级市商品销售额热力地图\"),\n",
    "    visualmap_opts=opts.VisualMapOpts(max_=max([sales for _, sales in city_sales]))\n",
    ")\n",
    "# 设置地图标记点（添加经纬度信息）\n",
    "for city, sales in city_sales:\n",
    "    if city in city_coordinate_map:\n",
    "        lng, lat = city_coordinate_map[city]\n",
    "        map_chart.add_coordinate(city, lng, lat)\n",
    "# 渲染生成HTML文件\n",
    "map_chart.render(\"2020年湖南省各地级市商品销售额热力地图.html\")\n",
    "print(\"信计221 刘显婷 224180117\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "abde2b5e-c2a3-4b1b-b2c5-c6bb713f69d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "信计221 刘显婷 224180117\n"
     ]
    }
   ],
   "source": [
    "from pyecharts.charts import Map\n",
    "from pyecharts import options as opts\n",
    "import re\n",
    "# 读取数据文件\n",
    "with open('湖南省数据.txt', 'r', encoding='utf-8') as f:\n",
    "    content = f.read()\n",
    "# 提取2020年商品利润额数据\n",
    "profit_pattern = r'2020年商品利润额：\\s*\\[(.*?)\\]'\n",
    "profit_match = re.search(profit_pattern, content, re.DOTALL)\n",
    "if profit_match:\n",
    "    profit_data_str = profit_match.group(1)\n",
    "    profit_data = eval(f'[{profit_data_str}]')\n",
    "else:\n",
    "    raise ValueError('未找到2020年商品利润额数据')\n",
    "# 提取地级市名称和利润额\n",
    "city_profit = [(city, profit) for city, profit in profit_data]\n",
    "# 创建Map对象\n",
    "map_chart = Map()\n",
    "# 添加数据和设置地图属性\n",
    "map_chart.add(\"2020年湖南省各地级市商品利润额\", city_profit, \"湖南\")\n",
    "# 设置全局配置项\n",
    "map_chart.set_global_opts(\n",
    "    title_opts=opts.TitleOpts(title=\"2020年湖南省各地级市商品利润额着色地图\"),\n",
    "    visualmap_opts=opts.VisualMapOpts(\n",
    "        min_=0,\n",
    "        max_=20,\n",
    "        range_color=[\"#E0FFFF\", \"#98FB98\", \"#FFD700\", \"#FFA500\", \"#FF0000\"]\n",
    "    )\n",
    ")\n",
    "# 渲染生成HTML文件\n",
    "map_chart.render(\"2020年湖南省各地级市商品利润额着色地图.html\")\n",
    "print(\"信计221 刘显婷 224180117\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "cbd4b7cb-afc4-4028-9f88-f0828ddd8b01",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "信计221 刘显婷 224180117\n"
     ]
    }
   ],
   "source": [
    "from pyecharts.charts import Map3D\n",
    "from pyecharts import options as opts\n",
    "# 13个地级市\n",
    "cities = [\"长沙市\", \"株洲市\", \"湘潭市\", \"衡阳市\", \"邵阳市\", \"岳阳市\", \"常德市\", \"张家界市\", \"益阳市\", \"郴州市\", \"永州市\", \"怀化市\", \"娄底市\"]\n",
    "# 经纬度坐标，这里简单取每组第一个坐标作为示例，你可以按需调整\n",
    "coordinates = [\n",
    "    [110.448719, 29.299332],\n",
    "    [111.718132, 29.218665],\n",
    "    [113.02434, 29.557034],\n",
    "    [112.601203, 27.132036],\n",
    "    [113.09793, 25.807389],\n",
    "    [111.626146, 26.60403],\n",
    "    [109.970389, 27.591986]\n",
    "]\n",
    "# 确保城市和坐标数量一致，这里简单处理，实际可能需要补充完整坐标\n",
    "if len(coordinates) < len(cities):\n",
    "    # 用最后一个坐标补齐缺失的部分，你可以根据实际情况修改\n",
    "    coordinates.extend([coordinates[-1]] * (len(cities) - len(coordinates)))\n",
    "# 构建城市与坐标的映射\n",
    "city_coordinate_map = dict(zip(cities, coordinates))\n",
    "# 创建 Map3D 对象\n",
    "map_3d = Map3D()\n",
    "# 添加数据\n",
    "for city, (lng, lat) in city_coordinate_map.items():\n",
    "    map_3d.add_schema(\n",
    "        itemstyle_opts=opts.ItemStyleOpts(color=\"#008080\"),\n",
    "        maptype=\"湖南\"\n",
    "    )\n",
    "    map_3d.add(\n",
    "        series_name=\"各地级市地理位置\",\n",
    "        data_pair=[(city, [lng, lat])],\n",
    "        type_=\"scatter3D\",\n",
    "        label_opts=opts.LabelOpts(is_show=False)\n",
    "    )\n",
    "# 设置全局配置项\n",
    "map_3d.set_global_opts(\n",
    "    title_opts=opts.TitleOpts(title=\"湖南省各地级市地理位置的三维地图\"),\n",
    "    visualmap_opts=opts.VisualMapOpts(\n",
    "        min_=0,\n",
    "        max_=100\n",
    "    )\n",
    ")\n",
    "# 设置动画效果\n",
    "map_3d.set_series_opts(\n",
    "    animation_opts=opts.AnimationOpts(\n",
    "        animation=True,\n",
    "        animation_delay=500,\n",
    "        animation_easing=\"linear\"\n",
    "    )\n",
    ")\n",
    "# 渲染生成 HTML 文件\n",
    "map_3d.render(\"湖南省各地级市地理位置的三维动态地图.html\")\n",
    "print(\"信计221 刘显婷 224180117\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe31d082-3871-4fa5-85b7-0f9fa63d0b64",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
